<template>
  <el-card>
    <el-form inline="">
      <el-form-item label="一级分类">
        <el-select v-model="categoryStore.c1Id" @change="handler">
          <!-- label:即为展示数据 value:即为select下拉菜单收集的数据 -->
          <el-option v-for="(c1, index) in categoryStore.c1Arr" :disabled="scene == 0 ? false : true" :key="c1.id" :label="c1.name" :value="c1.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="二级分类">
        <el-select v-model="categoryStore.c2Id" @change="handler1">
          <!-- label:即为展示数据 value:即为select下拉菜单收集的数据 -->
          <el-option v-for="(c2, index) in categoryStore.c2Arr" :disabled="scene == 0 ? false : true" :key="c2.id" :label="c2.name" :value="c2.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="三级分类">
        <el-select v-model="categoryStore.c3Id">
          <!-- label:即为展示数据 value:即为select下拉菜单收集的数据 -->
          <el-option v-for="(c3, index) in categoryStore.c3Arr" :disabled="scene == 0 ? false : true" :key="c3.id" :label="c3.name" :value="c3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: 'Category'
}
</script>
<script setup lang="ts">
//引入组件挂载完毕方法
import { onMounted, ref } from 'vue'
defineProps(['scene'])
//引入分类相关的仓库
import useCategoryStore from '@/store/modules/category'

let categoryStore = useCategoryStore()
//分类全局组件挂载完毕,通知仓库发请求获取一级分类的数据
onMounted(() => {
  getC1()
})

//通知仓库获取一级分类的方法
const getC1 = () => {
  //通知分类仓库发请求获取一级分类的数据
  categoryStore.getC1()
}

const handler = () => {
  //需要将二级、三级分类的数据清空
  categoryStore.c2Id = ''
  categoryStore.c3Arr = []
  categoryStore.c3Id = ''
  //通知仓库获取二级分类的数据
  categoryStore.getC2()
}
const handler1 = () => {
  //清理三级分类的数据
  categoryStore.c3Id = ''
  categoryStore.getC3()
}
</script>

<style lang="scss" scoped>
.el-form--inline .el-form-item {
  width: 200px;
}
</style>
